/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jmooc.servlets;

import com.jmooc.entity.ReadCourse;
import com.jmooc.entity.User;
import com.jmooc.entity.UserCertification;
import com.jmooc.service.ReadCourseService;
import com.jmooc.service.UserCertificationService;
import com.jmooc.service.UserService;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Group
 */
@WebServlet(name = "MyCourseServlets", urlPatterns = {"/mycourses"} )
public class MyCourseServlet extends HttpServlet {

    @EJB
    private UserService userService;
    
    @EJB
    private ReadCourseService readCourseService;
    
    @EJB
    private UserCertificationService userCertificationService;
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        
        List<ReadCourse> readCourses = null;
        List<UserCertification> certifications = null;
        
        try {
            User user = userService.getById((Long)session.getAttribute("userId"));

            try {
                certifications = userCertificationService.getAllByUser(user);
                request.setAttribute("certifications", certifications);
            } catch (Exception e) {
            }
            
            try {
                readCourses = readCourseService.getAllByUser(user);
                request.setAttribute("readCourses", readCourses);
            } catch (Exception e) {
            }
            
            request.setAttribute("user", user);
        } catch (Exception e) {
            session.setAttribute("message", "Erreur dans la certification");
        }
        
        request.getRequestDispatcher("/jsp/mycourses.jsp").forward(request, response);
    }

}
